﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.ServiceLocation;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core.Entities;
using YoProgramo.Domain.Core.Interfaces;

namespace YoProgramo.Domain.BusinessLogic.Extensions
{
    public static class StoryExtension {
        public static bool HasSpam(this IStory story) {
            var service = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Story>>();
            return service.IsSpam((Story) story);
        }

        public static void MarkAsSpam(this IStory story, IUser user, string ip,  bool approved) {
            var spam = new MarkAsSpam<Story>();
            spam.ByUser = user;
            spam.Approved = approved;
            spam.FromIPAddress = ip;
            spam.MarkedAt = DateTime.Now;
            spam.SpamFor = (Story) story;

            var service = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Story>>();
            service.Create(spam);
        }

        public static bool IsFavorite(this IStory story) {            
            if(HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var userservice = ServiceLocator.Current.GetInstance<IUserService>();
                IUser user = userservice.GetByUserName(HttpContext.Current.User.Identity.Name);
                return user.FavoritesStories.Contains(story);                
            }
            return false;

        }

        public static int FavoriteCount(this IStory story) {
            var storyservice = ServiceLocator.Current.GetInstance<IStoryService>();
            return storyservice.GetUsersFavForStory(story).Count;
        }

        public static void Vote(this IStory story, IUser user, string ip, int rate) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Story>>();
            service.VoteFor((Story)story, rate, user, ip);
        }

        public static bool CanVote(this IStory story,  IUser user) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Story>>();

            return !service.HasVoted((Story)story, user) || user.IsSpammer();
        }

        public static int GetScore(this IStory story) {
            var service = ServiceLocator.Current.GetInstance<IVoteService<Story>>();
            return service.GetScore((Story)story);            
        }

        public static int GetVisitsCount(this IStory story) {
            var service = ServiceLocator.Current.GetInstance<IVisitService<Story>>();
            return service.GetAllBy((Story)story).Count;
        }

        public static int GetTimeSinceInHours(this IStory story) {
            TimeSpan tsSinceFromRegistered = DateTime.Now.Subtract(story.PostedAt);
            int hoursSinceFromRegistered = tsSinceFromRegistered.Hours;
            if (hoursSinceFromRegistered == 0) return 1;

            return hoursSinceFromRegistered;
        }

        public static int GetWeightFactor(this IStory story) {
            var visit = GetVisitsCount(story);
            var score = GetScore(story);
            int since = GetTimeSinceInHours(story);
            int commentscount = story.Comments.Count; // <-- Include spams

            int weightFactor = ((score + visit) / since) * commentscount;

            return weightFactor;
        }

    }
}